<template>
	<!-- 用户登录组件 -->
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<view class="tips-text">登录后仅享更多权益</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from "vuex"
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed:{
			...mapState("m_user",["redirectInfo"]),
		},
		methods:{
			...mapMutations("m_user",['updateUserInfo',"updateToken","updateRedirectInfo","updateTeacherInfo"]),
			getUserInfo(e){
				uni.showLoading()
				//判断是否获取用户信息成功
				if(e.detail.errMsg == 'getUserInfo:fail auth deny') return uni.$showMsg("您取消了登录授权！"); 
				//获取用户信息成功，将用户的信息保存到vuex中
				this.updateUserInfo(e.detail.userInfo);
				//获取用户登录后的Token字符串
				this.getToken(e.detail);
				
			},
			/**
			 * 登录
			 * @param {Object} info 用户信息
			 */
			 async getToken(info){
				//调用微信登录接口
				 const res = await uni.login();
				 //判断是否wx.login()调用失败
				 if(!res || res.errMsg !== "login:ok") return uni.$showMsg("登录失败！");
				 
				 //准备参数对象
				 const query = {
					code:res.code,
					encryptedData:info.encryptedData, 
					iv:info.iv,
					rawData:info.rawData,
					signature:info.signature
				 }
				 //换取token
				 const {data :loginResult} = await uni.$http.post("Users/logins",query)
				 if(!loginResult || loginResult.meta.status !== 1) return uni.$showMsg("登录失败！")
				 uni.hideLoading()
				 //登录成功之后需要立马完善个人信息并且输入推荐码
				 // 判断用户信息是否齐全
				 let $user_info = loginResult.data.userinfo;
				 if(($user_info.name == null || $user_info.name.length == 0)  && ($user_info.phone == null || $user_info.phone.length == 0) && ($user_info.checkinvitationcode == null || $user_info.checkinvitationcode.length == 0)){
					 this.updateUserInfo($user_info)
					 uni.navigateTo({
					 	url:"/subpkg/refine_data/refine_data"
					 })	
				 }else{
					this.updateUserInfo($user_info)
				    this.updateToken($user_info.token)
					this.updateTeacherInfo(loginResult.data.teacherinfo)
				    uni.$showMsg("登录成功") 
					this.navigateBack()
				}
	
			},
			/**
			 * 返回登录前的页面
			 */
			 navigateBack(){
				//redirectInfo 不为Null 并且导航方式为 switchTab
				if(this.redirectInfo && this.redirectInfo.openType == 'switchTab'){
					
					//调用小程序提供的uni.switchTab() API进行页面的导航
					  uni.switchTab({
						//要导航到的页面地址
						url:this.redirectInfo.from,
						complete: () => {
							console.log(456)
							this.updateRedirectInfo(null)
						}
					  });
					console.log(typeof this.redirectInfo.from);
					console.log(this.redirectInfo.from);
				}
		
			}
		}
	}
</script>

<style lang="scss">
.login-container{
	height:950rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background-color: #e9e9e9;
	position:relative;
	overflow:hidden;
	&::after{
		content: '';
		display: block;
		width: 100%;
		height: 60px;
		background-color:#f8f8f8;
		position:absolute;
		bottom:0;
		left:0;
		border-radius: 100%;
		transform: translateY(50%);
		
		
	}
	.btn-login{
		width: 90%;
		border-radius: 100px;
		margin:15px 0;
		background-color: #d11a2d;
	}
	.tips-text{
		color: #aaaaaa;
		font-size:30rpx;
	}
}

</style>